.class public Lcom/snowplowanalytics/snowplow/tracker/Session;
.super Ljava/lang/Object;


# static fields
.field private static TAG:Ljava/lang/String;


# instance fields
.field private accessedLast:J

.field private backgroundTimeout:J

.field private context:Landroid/content/Context;

.field private currentSessionId:Ljava/lang/String;

.field private firstId:Ljava/lang/String;

.field private foregroundTimeout:J

.field private isBackground:Ljava/util/concurrent/atomic/AtomicBoolean;

.field private previousSessionId:Ljava/lang/String;

.field private sessionIndex:I

.field private sessionStorage:Ljava/lang/String;

.field private userId:Ljava/lang/String;


# direct methods
.method static constructor <clinit>()V
    .locals 1

    const-class v0, Lcom/snowplowanalytics/snowplow/tracker/Session;

    invoke-virtual {v0}, Ljava/lang/Class;->getSimpleName()Ljava/lang/String;

    move-result-object v0

    sput-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    return-void
.end method

.method public constructor <init>(JJLjava/util/concurrent/TimeUnit;Landroid/content/Context;)V
    .locals 5

    const/4 v1, 0x0

    const/4 v4, 0x0

    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    iput-object v1, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;

    iput v4, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    const-string v0, "SQLITE"

    iput-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionStorage:Ljava/lang/String;

    iput-object v1, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->firstId:Ljava/lang/String;

    new-instance v0, Ljava/util/concurrent/atomic/AtomicBoolean;

    invoke-direct {v0, v4}, Ljava/util/concurrent/atomic/AtomicBoolean;-><init>(Z)V

    iput-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->isBackground:Ljava/util/concurrent/atomic/AtomicBoolean;

    invoke-virtual {p5, p1, p2}, Ljava/util/concurrent/TimeUnit;->toMillis(J)J

    move-result-wide v0

    iput-wide v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->foregroundTimeout:J

    invoke-virtual {p5, p3, p4}, Ljava/util/concurrent/TimeUnit;->toMillis(J)J

    move-result-wide v0

    iput-wide v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->backgroundTimeout:J

    iput-object p6, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->context:Landroid/content/Context;

    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->getSessionFromFile()Ljava/util/Map;

    move-result-object v0

    if-nez v0, :cond_0

    invoke-static {}, Lcom/snowplowanalytics/snowplow/tracker/utils/Util;->getEventId()Ljava/lang/String;

    move-result-object v0

    iput-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->userId:Ljava/lang/String;

    :goto_0
    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->updateSessionInfo()V

    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->updateAccessedTime()V

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Tracker Session Object created."

    new-array v2, v4, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->v(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    return-void

    :cond_0
    :try_start_0
    const-string v1, "userId"

    invoke-interface {v0, v1}, Ljava/util/Map;->get(Ljava/lang/Object;)Ljava/lang/Object;

    move-result-object v1

    invoke-virtual {v1}, Ljava/lang/Object;->toString()Ljava/lang/String;

    move-result-object v1

    const-string v2, "sessionId"

    invoke-interface {v0, v2}, Ljava/util/Map;->get(Ljava/lang/Object;)Ljava/lang/Object;

    move-result-object v2

    invoke-virtual {v2}, Ljava/lang/Object;->toString()Ljava/lang/String;

    move-result-object v2

    const-string v3, "sessionIndex"

    invoke-interface {v0, v3}, Ljava/util/Map;->get(Ljava/lang/Object;)Ljava/lang/Object;

    move-result-object v0

    check-cast v0, Ljava/lang/Integer;

    invoke-virtual {v0}, Ljava/lang/Integer;->intValue()I

    move-result v0

    iput-object v1, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->userId:Ljava/lang/String;

    iput v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    iput-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    goto :goto_0

    :catch_0
    move-exception v0

    sget-object v1, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v2, "Exception occurred retrieving session info from file: %s"

    const/4 v3, 0x1

    new-array v3, v3, [Ljava/lang/Object;

    invoke-virtual {v0}, Ljava/lang/Exception;->getMessage()Ljava/lang/String;

    move-result-object v0

    aput-object v0, v3, v4

    invoke-static {v1, v2, v3}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->e(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    invoke-static {}, Lcom/snowplowanalytics/snowplow/tracker/utils/Util;->getEventId()Ljava/lang/String;

    move-result-object v0

    iput-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->userId:Ljava/lang/String;

    goto :goto_0
.end method

.method private getSessionFromFile()Ljava/util/Map;
    .locals 2

    const-string v0, "snowplow_session_vars"

    iget-object v1, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->context:Landroid/content/Context;

    invoke-static {v0, v1}, Lcom/snowplowanalytics/snowplow/tracker/utils/FileStore;->getMapFromFile(Ljava/lang/String;Landroid/content/Context;)Ljava/util/Map;

    move-result-object v0

    return-object v0
.end method

.method private saveSessionToFile()Z
    .locals 3

    const-string v0, "snowplow_session_vars"

    invoke-virtual {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->getSessionValues()Ljava/util/Map;

    move-result-object v1

    iget-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->context:Landroid/content/Context;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/FileStore;->saveMapToFile(Ljava/lang/String;Ljava/util/Map;Landroid/content/Context;)Z

    move-result v0

    return v0
.end method

.method private updateAccessedTime()V
    .locals 2

    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v0

    iput-wide v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->accessedLast:J

    return-void
.end method

.method private updateSessionInfo()V
    .locals 6

    const/4 v5, 0x1

    const/4 v4, 0x0

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;

    iput-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->previousSessionId:Ljava/lang/String;

    invoke-static {}, Lcom/snowplowanalytics/snowplow/tracker/utils/Util;->getEventId()Ljava/lang/String;

    move-result-object v0

    iput-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;

    iget v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    add-int/lit8 v0, v0, 0x1

    iput v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Session information is updated:"

    new-array v2, v4, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, " + Session ID: %s"

    new-array v2, v5, [Ljava/lang/Object;

    iget-object v3, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;

    aput-object v3, v2, v4

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, " + Previous Session ID: %s"

    new-array v2, v5, [Ljava/lang/Object;

    iget-object v3, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->previousSessionId:Ljava/lang/String;

    aput-object v3, v2, v4

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, " + Session Index: %s"

    new-array v2, v5, [Ljava/lang/Object;

    iget v3, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    invoke-static {v3}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer;

    move-result-object v3

    aput-object v3, v2, v4

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->saveSessionToFile()Z

    return-void
.end method


# virtual methods
.method public checkAndUpdateSession()V
    .locals 8

    const/4 v7, 0x0

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Checking and updating session information."

    new-array v2, v7, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v2

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->isBackground:Ljava/util/concurrent/atomic/AtomicBoolean;

    invoke-virtual {v0}, Ljava/util/concurrent/atomic/AtomicBoolean;->get()Z

    move-result v6

    if-eqz v6, :cond_1

    iget-wide v4, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->backgroundTimeout:J

    :goto_0
    iget-wide v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->accessedLast:J

    invoke-static/range {v0 .. v5}, Lcom/snowplowanalytics/snowplow/tracker/utils/Util;->isTimeInRange(JJJ)Z

    move-result v0

    if-nez v0, :cond_0

    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->updateSessionInfo()V

    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->updateAccessedTime()V

    if-eqz v6, :cond_0

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Timeout in background, pausing session checking..."

    new-array v2, v7, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    :try_start_0
    invoke-static {}, Lcom/snowplowanalytics/snowplow/tracker/Tracker;->instance()Lcom/snowplowanalytics/snowplow/tracker/Tracker;

    move-result-object v0

    invoke-virtual {v0}, Lcom/snowplowanalytics/snowplow/tracker/Tracker;->pauseSessionChecking()V
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    :cond_0
    :goto_1
    return-void

    :cond_1
    iget-wide v4, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->foregroundTimeout:J

    goto :goto_0

    :catch_0
    move-exception v0

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Could not pause checking as tracker not setup"

    new-array v2, v7, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->e(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    goto :goto_1
.end method

.method public getBackgroundTimeout()J
    .locals 2

    iget-wide v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->backgroundTimeout:J

    return-wide v0
.end method

.method public getCurrentSessionId()Ljava/lang/String;
    .locals 1

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;

    return-object v0
.end method

.method public getFirstId()Ljava/lang/String;
    .locals 1

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->firstId:Ljava/lang/String;

    return-object v0
.end method

.method public getForegroundTimeout()J
    .locals 2

    iget-wide v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->foregroundTimeout:J

    return-wide v0
.end method

.method public getPreviousSessionId()Ljava/lang/String;
    .locals 1

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->previousSessionId:Ljava/lang/String;

    return-object v0
.end method

.method public declared-synchronized getSessionContext(Ljava/lang/String;)Lcom/snowplowanalytics/snowplow/tracker/payload/SelfDescribingJson;
    .locals 3

    monitor-enter p0

    :try_start_0
    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Getting session context..."

    const/4 v2, 0x0

    new-array v2, v2, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->v(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    invoke-direct {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->updateAccessedTime()V

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->firstId:Ljava/lang/String;

    if-nez v0, :cond_0

    iput-object p1, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->firstId:Ljava/lang/String;

    :cond_0
    new-instance v0, Lcom/snowplowanalytics/snowplow/tracker/payload/SelfDescribingJson;

    const-string v1, "iglu:com.snowplowanalytics.snowplow/client_session/jsonschema/1-0-1"

    invoke-virtual {p0}, Lcom/snowplowanalytics/snowplow/tracker/Session;->getSessionValues()Ljava/util/Map;

    move-result-object v2

    invoke-direct {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/payload/SelfDescribingJson;-><init>(Ljava/lang/String;Ljava/lang/Object;)V
    :try_end_0
    .catchall {:try_start_0 .. :try_end_0} :catchall_0

    monitor-exit p0

    return-object v0

    :catchall_0
    move-exception v0

    monitor-exit p0

    throw v0
.end method

.method public getSessionIndex()I
    .locals 1

    iget v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    return v0
.end method

.method public getSessionStorage()Ljava/lang/String;
    .locals 1

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionStorage:Ljava/lang/String;

    return-object v0
.end method

.method public getSessionValues()Ljava/util/Map;
    .locals 3

    new-instance v0, Ljava/util/HashMap;

    invoke-direct {v0}, Ljava/util/HashMap;-><init>()V

    const-string v1, "userId"

    iget-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->userId:Ljava/lang/String;

    invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    const-string v1, "sessionId"

    iget-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->currentSessionId:Ljava/lang/String;

    invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    const-string v1, "previousSessionId"

    iget-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->previousSessionId:Ljava/lang/String;

    invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    const-string v1, "sessionIndex"

    iget v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionIndex:I

    invoke-static {v2}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer;

    move-result-object v2

    invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    const-string v1, "storageMechanism"

    iget-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->sessionStorage:Ljava/lang/String;

    invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    const-string v1, "firstEventId"

    iget-object v2, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->firstId:Ljava/lang/String;

    invoke-interface {v0, v1, v2}, Ljava/util/Map;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

    return-object v0
.end method

.method public getUserId()Ljava/lang/String;
    .locals 1

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->userId:Ljava/lang/String;

    return-object v0
.end method

.method public setIsBackground(Z)V
    .locals 5

    const/4 v4, 0x0

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Application is in the background: %s"

    const/4 v2, 0x1

    new-array v2, v2, [Ljava/lang/Object;

    invoke-static {p1}, Ljava/lang/Boolean;->valueOf(Z)Ljava/lang/Boolean;

    move-result-object v3

    aput-object v3, v2, v4

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->isBackground:Ljava/util/concurrent/atomic/AtomicBoolean;

    invoke-virtual {v0}, Ljava/util/concurrent/atomic/AtomicBoolean;->get()Z

    move-result v0

    if-eqz v0, :cond_0

    if-nez p1, :cond_0

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Application moved to foreground, starting session checking..."

    new-array v2, v4, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->d(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    :try_start_0
    invoke-static {}, Lcom/snowplowanalytics/snowplow/tracker/Tracker;->instance()Lcom/snowplowanalytics/snowplow/tracker/Tracker;

    move-result-object v0

    invoke-virtual {v0}, Lcom/snowplowanalytics/snowplow/tracker/Tracker;->resumeSessionChecking()V
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    :cond_0
    :goto_0
    iget-object v0, p0, Lcom/snowplowanalytics/snowplow/tracker/Session;->isBackground:Ljava/util/concurrent/atomic/AtomicBoolean;

    invoke-virtual {v0, p1}, Ljava/util/concurrent/atomic/AtomicBoolean;->set(Z)V

    return-void

    :catch_0
    move-exception v0

    sget-object v0, Lcom/snowplowanalytics/snowplow/tracker/Session;->TAG:Ljava/lang/String;

    const-string v1, "Could not resume checking as tracker not setup"

    new-array v2, v4, [Ljava/lang/Object;

    invoke-static {v0, v1, v2}, Lcom/snowplowanalytics/snowplow/tracker/utils/Logger;->e(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

    goto :goto_0
.end method
